#!/usr/bin/perl
#file : tcp_echo_serv2.pl
#usage: tcp_echo_serv2.pl [port]
use strict;
use IO::Socket qw(:DEFAULT :crlf);
use constant MY_ECHO_PORT => 2007;
# $/ = CRLF;        # 回车。。。影响getline函数判据
my ($bytes_out, $bytes_in) = (0, 0);

my $quit = 0;
$SIG{INT} = sub{$quit++};

my $port = shift || MY_ECHO_PORT;

my $sock = IO::Socket::INET->new(Listen => 20,
                                LocalPort => $port,
                                Timeout => 60*60,
                                Reuse => 1)
    or die "can't create listening socket $@\n";
warn "waiting for incoming connections on port $port...\n";
while(!$quit){
    next unless my $session = $sock->accept;

    my $peer = gethostbyaddr($session->peeraddr, AF_INET) ||
        $session->peeraddr;
    my $port = $session->peerport;
    warn "Connection from {$peer, $port}\n";
    while(<$session>){
    # while(defined($session->getline)){
        $bytes_in += length($_);
        chomp;
        my $msg_out = (scalar reverse $_) . CRLF;
        $session->send($msg_out);
        # print $session $msg_out;
        # print STDOUT $msg_out;
        $bytes_out += length($msg_out);
    }
    warn "Connection from [$peer, $port] finished\n";
    close $session;
}

print STDERR "bytes_sent $bytes_out, bytes_recv $bytes_in\n";
close $sock